package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.ConsumerPrivateQO;
import com.xingkeduo.pojo.table.ConsumerPrivateTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/3/1 14:54
 **/
@Service
public class ConsumerPrivateService {

    private static final String NAMESPACE = CommonUtil.formatNamespace(ConsumerPrivateTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<ConsumerPrivateTO, ConsumerPrivateQO> dao;

    public List<ConsumerPrivateTO> selectUserPrivateList(ConsumerPrivateQO qo) throws Throwable {
        return dao.select(NAMESPACE, qo);
    }

    public List<Long> selectUserPrivateForId(ConsumerPrivateQO qo) throws Throwable {
        return dao.selectIndex(NAMESPACE, qo);
    }

    public ConsumerPrivateTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    @Transactional
    public void update(ConsumerPrivateTO consumerPrivateTO) throws Throwable {
        if (dao.update(NAMESPACE, consumerPrivateTO) < 1)
            throw new DataException(DataException.Type.FAIL, "修改消费者私人信息");
    }

    @Transactional
    public void insert(ConsumerPrivateTO consumerPrivateTO) throws Throwable {
        if (dao.insert(NAMESPACE, consumerPrivateTO) < 1)
            throw new DataException(DataException.Type.FAIL, "新增消费者私人信息");
    }

    @Transactional
    public int updateList(List<ConsumerPrivateTO> list) throws Throwable {
        int count = 0;
        for (Iterator<ConsumerPrivateTO> iterator = list.iterator(); iterator.hasNext(); ) {
            count += dao.update(NAMESPACE, iterator.next());
        }
        return count;
    }
}
